home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / TGCBOR20.ARJ / TUTOR.COM / BUTTONS.TXT next >
Text File  |  1991-08-27  |  11KB  |  423 lines

  1. BUTTONS
  2. -------------------------------------------------------------
  3.  
  4. Buttons are things that you attach to frames. Pressing a button
  5. (by pointing with the mouse cursor and pressing the left
  6. mouse button) is an event, so a button must have an associated
  7. event-handler.
  8.  
  9.  
  10.  
  11. void definesquarebuttontext(imagestkptr ifs,
  12.                             unsigned x1, unsigned y1,
  13.                             unsigned x2, unsigned y2,
  14.                             unsigned rx, unsigned ry,
  15.                             char *msg, callproc event);
  16.  
  17. This will draw a square button on the frame ifs. x1, y1, x2, y2 are
  18. relative coordinates on the frame where the button will be drawn.
  19. rx and ry are offsets to draw the text msg at. Event is the
  20. event-handler to call when the button is pressed.
  21.  
  22. char visualsqarebuttonpress(imagestkptr ifs, msclickptr ms);
  23.  
  24. This function presses a squarebutton down and then returns TRUE if
  25. the mouse button was released while the mouse pointer was over the button.
  26. This function should be called from within an event-handler and would
  27. be passed the same parameters that were passed to the event-handler.
  28.  
  29. The event-handler would use the return value to determine if an action
  30. is required (i.e. if TRUE). If so then the first thing that should be
  31. done is a call to releasesquarebutton which will pop the button back
  32. up.
  33.  
  34. If visualsquarebuttonpress returns FALSE then releasesquarebutton is
  35. called automatically and the event-handler should not call it.
  36.  
  37. void releasesquarebutton(imagestkptr ifs, msclickptr ms);
  38.  
  39. This will release a button that was previously pressed by
  40. visualsquarebuttonpress.
  41.  
  42.  
  43. This example shows the complete use of a button attached to a frame.
  44. When the button is pressed and released then the frame is disposed
  45. of.
  46.  
  47. BEGINFILE> button1.c
  48. /* -- button1.c */
  49.  
  50. #include "teglsys.h"
  51.  
  52. imagestkptr  ifs;
  53.  
  54.  
  55. unsigned cancelevent(imagestkptr  frame, msclickptr   mouse)
  56.   {
  57.       /* -- if true then the button has been released  */
  58.     if (visualsquarebuttonpress(frame,mouse))
  59.       {
  60.       /* -- this will pop the button back up  */
  61.     releasesquarebutton(frame,mouse);
  62.       /* -- and dispose of the frame.  */
  63.     dropstackimage(ifs);
  64.       }
  65.     return 0;
  66.   }
  67.  
  68.  
  69. void main(void)
  70. {
  71.  
  72.   easytegl();
  73.   easyout();
  74.   pushimage(50,50,200,150);
  75.   shadowbox(50,50,200,150);
  76.   ifs = stackptr;
  77.   definesquarebuttontext(ifs,30,35,120,65,5,5,"PRESS ME",cancelevent);
  78.   teglsupervisor();
  79. }
  80.  
  81.  
  82.  
  83. ENDFILE>
  84.  
  85.  
  86. Buttons can have more than text displayed on them. In true GUI fashion
  87. buttons might have appropriate icons displayed on them to indicate
  88. their purpose.
  89.  
  90. void definesquarebuttonclick(imagestkptr ifs,
  91.                              unsigned x1, unsigned y1,
  92.                              unsigned x2, unsigned y2,
  93.                              unsigned rx, unsigned ry,
  94.                              char *button, callproc event);
  95.  
  96. definesquarebuttonclick is the same as definesquarebuttontext above
  97. except that an icon is displayed on the button instead of text.
  98.  
  99. This example creates a frame with squarebutton that shows a
  100. lightbulb icon.
  101.  
  102.  
  103. BEGINFILE> button2.c
  104. /* -- button2.c  */
  105.  
  106. #include "teglsys.h"
  107.  
  108. imagestkptr  ifs;
  109.  
  110.  
  111. unsigned cancelevent(imagestkptr  frame, msclickptr   mouse)
  112.   {
  113.       /* -- if true then the button has been released  */
  114.     if (visualsquarebuttonpress(frame,mouse))
  115.       {
  116.       /* -- this will pop the button back up  */
  117.     releasesquarebutton(frame,mouse);
  118.       /* -- and dispose of the frame.  */
  119.     dropstackimage(ifs);
  120.       }
  121.     return 0;
  122.   }
  123.  
  124.  
  125. void main(void)
  126. {
  127.  
  128.   easytegl();
  129.   easyout();
  130.   pushimage(50,50,200,150);
  131.   shadowbox(50,50,200,150);
  132.   ifs = stackptr;
  133.   definesquarebuttonclick(ifs,30,35,120,65,35,5,imageBULB,cancelevent);
  134.   teglsupervisor();
  135. }
  136.  
  137.  
  138.  
  139. ENDFILE>
  140.  
  141.  
  142. It may be useful at some time to have a button floating around by itself.
  143. The easyout button that is displayed at the bottom right corner of the screen
  144. is just such a button. In fact it is a frame with a button that is the full
  145. size of the frame. We can use these for other purposes.
  146.  
  147. This example places two floating buttons on the screen, one with
  148. a 5.25" floppy disk icon and the other with a 3.5" floppy disk icon.
  149.  
  150. BEGINFILE> button3.c
  151. /* -- button3.c */
  152.  
  153. #include "teglsys.h"
  154.  
  155.  
  156. unsigned disk35event(imagestkptr  frame, msclickptr   mouse)
  157.   {
  158.       /* -- if true then the button has been released  */
  159.     if (visualsquarebuttonpress(frame,mouse))
  160.       {
  161.       /* -- this will pop the button back up  */
  162.     releasesquarebutton(frame,mouse);
  163.       }
  164.     return 0;
  165.   }
  166.  
  167.  
  168. unsigned diskevent(imagestkptr  frame, msclickptr   mouse)
  169.   {
  170.       /* -- if true then the button has been released  */
  171.     if (visualsquarebuttonpress(frame,mouse))
  172.       {
  173.       /* -- this will pop the button back up  */
  174.       /*  ReleaseSquareButton(frame,mouse);  */
  175.       }
  176.     return 0;
  177.   }
  178.  
  179.  
  180.  
  181.  
  182. void main(void)
  183. {
  184.  
  185.   easytegl();
  186.   easyout();
  187.   pushimage(0,9,39,49);
  188.   definesquarebuttonclick(stackptr,0,0,39,39,5,5,imageDISK,diskevent);
  189.   pushimage(0,49,39,89);
  190.   definesquarebuttonclick(stackptr,0,0,39,39,5,5,imageDISK35,disk35event);
  191.  
  192.   teglsupervisor();
  193. }
  194.  
  195.  
  196.  
  197. ENDFILE>
  198.  
  199.  
  200. One of the things that can go wrong is forgetting to release a
  201. square button once it is pressed. Try commenting out one of the
  202. releasesquarebutton calls from the previous program and see what
  203. happens when the button is pressed repeatedly.
  204.  
  205.  
  206.  
  207. The next example gives a complete illustration of a floating button that
  208. opens up a window to do something. Important items to note here are:
  209.  
  210.   * the use of a control variable to keep track of a frame that you
  211.     don't want more than one instance running.
  212.   * setting the viewport to cover a frame, you don't have to calculate
  213.     the relative displacement for text or graphics then.
  214.   * using more than one button on a frame. Each with its own event.
  215.  
  216.  
  217. BEGINFILE> button4.c
  218.   /* -- button4.c */
  219.  
  220. #include "teglsys.h"
  221.  
  222.   /* -- a control variable to make sure that only one instance of the  */
  223.   /* -- format frame is displayed  */
  224.  
  225.  
  226. unsigned char formatinstance   = FALSE;
  227.  
  228.  
  229. unsigned formatevent(imagestkptr  frame, msclickptr   mouse)
  230.   {
  231.     if (visualsquarebuttonpress(frame,mouse))
  232.       {
  233.     releasesquarebutton(frame,mouse);
  234.     setviewport(0,0,getmaxx(),getmaxy(),FALSE);
  235.     errmess(getmaxx() / 4,getmaxy() / 4,"Function not compete!");
  236.       }
  237.     return 0;
  238.   }
  239.  
  240.  
  241. unsigned cancelevent(imagestkptr  frame, msclickptr   mouse)
  242.   {
  243.     if (visualsquarebuttonpress(frame,mouse))
  244.       {
  245.     releasesquarebutton(frame,mouse);
  246.     dropstackimage(frame);
  247.     formatinstance = FALSE;
  248.       }
  249.     return 0;
  250.   }
  251.  
  252.  
  253.  
  254. unsigned diskevent(imagestkptr  frame, msclickptr mouse)
  255.   { imagestkptr  ifs;
  256.     unsigned x1 = 100;
  257.     unsigned y1 = 100;
  258.     unsigned x2 = 200;
  259.     unsigned y2 = 100;
  260.  
  261.       /* -- if true then the button has been released  */
  262.     if (visualsquarebuttonpress(frame,mouse))
  263.       {
  264.       /* -- this will pop the button back up  */
  265.     releasesquarebutton(frame,mouse);
  266.     if (formatinstance)
  267.       {
  268.         setviewport(0,0,getmaxx(),getmaxy(),FALSE);
  269.         errmess(200,100,"The format window is open");
  270.         return 0;
  271.       }
  272.     formatinstance = TRUE;
  273.     setviewport(0,0,getmaxx(),getmaxy(),FALSE);   /* -- fullscreen  */
  274.       /* -- upper corner  */
  275.     quickframe(&ifs,&x1, &y1, &x2, &y2);   /* -- width & height  */
  276.     setteglfont(font14);
  277.       /* -- setting the viewport to the frame makes it easy  */
  278.       /* -- for outputting text and graphics.  */
  279.     prepareforupdate(ifs);
  280.     setviewport(ifs->x,ifs->y,ifs->x1,ifs->y1,TRUE);
  281.     settextjustify(CENTER_TEXT,TOP_TEXT);
  282.     outtextxy(100,20,"Format a disk?");
  283.     settextjustify(LEFT_TEXT,TOP_TEXT);
  284.     commitupdate();
  285.  
  286.     definesquarebuttontext(ifs,20,70,60,90,10,5,"OK",formatevent);
  287.     definesquarebuttontext(ifs,110,70,180,90,10,5,"CANCEL",cancelevent);
  288.  
  289.       }
  290.     return 0;
  291.   }
  292.  
  293.  
  294.  
  295.  
  296. void main(void)
  297. {
  298.  
  299.   easytegl();
  300.   easyout();
  301.   setautorotate(TRUE);
  302.   pushimage(0,9,39,49);
  303.   definesquarebuttonclick(stackptr,0,0,39,39,6,4,imageDISK,diskevent);
  304.   setteglfont(f5x6norm);
  305.   setcolor(BLACK);
  306.   outtextxy(4,33,"Format");
  307.   outtextxy(9,40,"Disk");
  308.   teglsupervisor();
  309. }
  310.  
  311.  
  312.  
  313. ENDFILE>
  314.  
  315. This example illustrates how mouse click numbers are associated
  316. with a button. It draws frame with fifty number buttons on it that
  317. display their button numbers when clicked on.
  318.  
  319. This shows how easy it is to set up a matrix of buttons. The important
  320. part of this program is how the event-handler (shownumber) can determine
  321. which button was pressed. Consider how straight forward it would be
  322. to implement a calculator.
  323.  
  324.  
  325. BEGINFILE> button5.c
  326.   /* -- button5.c */
  327.  
  328. #include "teglsys.h"
  329.  
  330.  
  331. unsigned shownumber( imagestkptr frame, msclickptr mouse )
  332.   {
  333.     char s[25];
  334.       /* -- if true then the button has been released  */
  335.     if (visualsquarebuttonpress(frame,mouse))
  336.       {
  337.       /* -- this will pop the button back up  */
  338.     releasesquarebutton(frame,mouse);
  339.     prepareforupdate(frame);
  340.     setviewport(frame->x,frame->y,frame->x1,frame->y1,TRUE);
  341.     setfillstyle(SOLID_FILL,WHITE);
  342.     bar(5,110,210,130);
  343.     setteglfont(f8x12bol);
  344.       /* -- the mouse pointer contains what clicknumber it was  */
  345.       /* -- assigned to.  */
  346. setcolor(BLACK);
  347.     outtextxy(5,112,"Button ");
  348.     itoa(mouse->clicknumber,s,10);
  349.     outtextxy(70,112,s);
  350.     commitupdate();
  351.       }
  352.     return 0;
  353.   }
  354.  
  355.  
  356. void         buttonbunch(void)
  357. { int      i, xd, yd;
  358.     imagestkptr  ifs;
  359.     unsigned x1 = 100;
  360.     unsigned y1 = 100;
  361.     unsigned x2 = 222;
  362.     unsigned y2 = 140;
  363.     char s[25];
  364.  
  365.     setviewport(0,0,getmaxx(),getmaxy(),FALSE);
  366.     quickframe(&ifs,&x1,&y1,&x2,&y2);
  367.     xd = 0; yd = 0;
  368.     for (i = 1; i <= 50; i++)
  369.       {
  370.       /* -- as we define each button we put its clicknumber on it */
  371.       /* -- note that MsClickCount would only be the correct on return */
  372.       /* -- so we add one to it.  */
  373.     setteglfont(f7x7bold);
  374.     itoa(+ifs->msclickcount + 1,s,10);
  375.     definesquarebuttontext(ifs,xd,yd,xd + 20,yd + 20,3,8,s,shownumber);
  376.     xd = xd + 22;
  377.     if (xd > 200)
  378.       {
  379.         xd = 0;
  380.         yd = yd + 22;
  381.       }
  382.       }
  383.   }
  384.  
  385.  
  386.  
  387. void main(void)
  388. {
  389.  
  390.   easytegl();
  391.   easyout();
  392.   buttonbunch();
  393.   teglsupervisor();
  394. }
  395.  
  396.  
  397.  
  398. ENDFILE>
  399.  
  400.  
  401. Things to remember!
  402.  
  403.     * Square button arguments are RELATIVE to the frame they are being
  404.       attached to.
  405.  
  406.     * A square button is just another mouse click on a frame. The
  407.       imagestkptr that is passed to definesquarebuttonclick or
  408.       definesquarebuttontext will contain the clicknumber of the
  409.       on return in the msclickcount field.
  410.  
  411.     * You can make floating buttons by making the button and the frame
  412.       the same size.
  413.  
  414.     * Beware of viewport settings. Many routines assume that the
  415.       viewport and text settings are the startup defaults. If in doubt
  416.       do a setviewport(0,0,getmaxx(),getmaxy(),0) and
  417.       settextjustify(LEFT_TEXT,TOP_TEXT);
  418.  
  419. ----------------------------------------------------------
  420. END buttons.txt
  421.  
  422.  
  423.